package com.example.administrator.guosai30;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.nle.mylibrary.forUse.mdbus4150.MdBus4150RelayListener;
import com.nle.mylibrary.forUse.mdbus4150.Modbus4150;
import com.nle.mylibrary.forUse.rfid.RFID;
import com.nle.mylibrary.forUse.rfid.SingleEpcListener;
import com.nle.mylibrary.transfer.DataBusFactory;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private RFID rfid;
    private Modbus4150 modbus4150;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rfid=new RFID(DataBusFactory.newSocketDataBus("172.21.2.202",4196));
        modbus4150=new Modbus4150(DataBusFactory.newSocketDataBus("172.21.2.201",4196));
        Timer timer=new Timer();
        Timer timer2=new Timer();
        TimerTask timerTask=new TimerTask() {
            @Override
            public void run() {
                Log.i("timer","1");
                try {
                    modbus4150.closeRelay(3, new MdBus4150RelayListener() {
                        @Override
                        public void onCtrl(boolean isSuccess) {
                            Log.i("main",isSuccess+"");
                        }
                    });
                    modbus4150.openRelay(2, new MdBus4150RelayListener() {
                        @Override
                        public void onCtrl(boolean isSuccess) {
                            Log.i("main",isSuccess+"");
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        TimerTask timerTask2=new TimerTask() {
            @Override
            public void run() {
                Log.i("timer2","1");

                try {
                    rfid.readSingleEpc(new SingleEpcListener() {
                        @Override
                        public void onVal(String val) {
                            Log.i("val:",val);
                            if (!val.equals("")){
                                try {
                                    modbus4150.closeRelay(2, new MdBus4150RelayListener() {
                                        @Override
                                        public void onCtrl(boolean isSuccess) {
                                            Log.i("main",isSuccess+"");
                                        }
                                    });
                                    modbus4150.openRelay(3, new MdBus4150RelayListener() {
                                        @Override
                                        public void onCtrl(boolean isSuccess) {
                                            Log.i("main",isSuccess+"");
                                        }
                                    });
                                    timer2.schedule(timerTask,10000);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        timer.schedule(timerTask2,0,1000);
    }
}
